package com.net.shop.car.manager.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.BaseFragment;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.VersionInfo;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.GetHeadpictures;
import com.net.shop.car.manager.api.volley.response.GetHeadPicturesResponse;
import com.net.shop.car.manager.ui.chezhoubian.ChezhoubianDialog;
import com.net.shop.car.manager.ui.guaguale.GuagualeFragment;
import com.net.shop.car.manager.ui.head.HeadFragment;
import com.net.shop.car.manager.ui.personalcenter.PersonalCenterFragment;
import com.net.shop.car.manager.ui.view.SwitchWidget;
import com.net.shop.car.manager.ui.view.SwitchWidget.OnSwitchedListener;
import com.net.shop.car.manager.utils.payment.PaymentUtils;

/**
 * @description 主页面activty，承载用户接口的主要窗口
 * @author Deve
 * @date 2015-01-17
 */
public class MainActivity extends BaseActivity implements OnSwitchedListener {

	/**
	 * 底部菜单
	 */
	private SwitchWidget mBottomMenu;

	/**
	 * 首页
	 */
	private HeadFragment mHeadFragment;
	/**
	 * 个人中心
	 */
	private PersonalCenterFragment mCenterFragment;
	/**
	 * 刮刮乐
	 */
	private GuagualeFragment mGuagualeFragment;

	/**
	 * 当前显示的碎片
	 */
	private BaseFragment mCurrentShowFragment;
	/**
	 * 
	 */
	private final int MENU_HEAD = 0;
	/**
	 * 
	 */
	private final int MENU_LAUNCHER = 1;
	/**
	 * 
	 */
	private final int MENU_PERSONALCENTER = 3;
	/**
	 * 
	 */
	private final int MENU_GUAGUALE = 2;
	/**
	 * 
	 */
	private int currentIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		PaymentUtils.getInstance().setContext(this);
		
		initViews();

//		LocationService.ls().init(this);
		VolleyCenter.getVolley().addGetRequest(
				new GetHeadpictures("-1"),
				new VolleyListenerImpl<GetHeadPicturesResponse>(
						new GetHeadPicturesResponse()) {

					@Override
					public void deleveryResponse(
							GetHeadPicturesResponse response) {

						VersionInfo versionInfo = response.getVersionInfo();
						if (versionInfo != null) {
							notifyVersion(versionInfo);
						}
						mHeadFragment.notifyHeadPictures(response
								.getHeadPictures());
					}
				});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

//		LocationService.ls().dettach();
	}

	/**
	 * 初始化view
	 */
	private void initViews() {
		mHeadFragment = new HeadFragment();

		mCenterFragment = new PersonalCenterFragment();

		mGuagualeFragment = new GuagualeFragment();
		// 将fragment加入
		fragmentManager.beginTransaction()
				.add(R.id.main_fragcontainer, mHeadFragment)
				.add(R.id.main_fragcontainer, mCenterFragment)
				.add(R.id.main_fragcontainer, mGuagualeFragment)
				.hide(mCenterFragment).hide(mGuagualeFragment)
				.hide(mHeadFragment).commit();
		mCurrentShowFragment = mHeadFragment;
		
		mBottomMenu = (SwitchWidget) findViewById(R.id.main_bottom);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mBottomMenu.postDelayed(new Runnable() {

			@Override
			public void run() {
				mBottomMenu.setOnSwitchedListener(MainActivity.this);
			}
		}, 20);
	}

	@Override
	public void onItemSelected(int index) {
		switch (index) {
		case MENU_HEAD:
			showFragment(mHeadFragment);
			break;
		case MENU_LAUNCHER:
			gotoCheZhouBian();
			return;
		case MENU_PERSONALCENTER:
			showFragment(mCenterFragment);
			break;
		case MENU_GUAGUALE:
			showFragment(mGuagualeFragment);
			break;
		default:
			break;
		}
		currentIndex = index;
	}

	/**
	 * 
	 */
	public void gotoCheZhouBian() {
		ChezhoubianDialog chezhoubianDialog = new ChezhoubianDialog(this);
		chezhoubianDialog
				.setOnDismissListener(new DialogInterface.OnDismissListener() {

					@Override
					public void onDismiss(DialogInterface dialog) {
						mBottomMenu.setItemSelected(currentIndex);
					}
				});
		chezhoubianDialog.show();
	}

	/**
	 * 
	 */
	public void notifyVersion(VersionInfo versionInfo) {
		// TODO 显示版本信息
	}

	/**
	 * 显示当前的fragment
	 * 
	 * @param fragment
	 *            BaseFragment
	 */
	private void showFragment(BaseFragment fragment) {
		fragmentManager.beginTransaction().hide(mCurrentShowFragment)
				.show(fragment).commitAllowingStateLoss();
		mCurrentShowFragment = fragment;
	}
}
